package tech.com.commoncore.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import tech.com.commoncore.R;


/**
 * Created by hhl on 2018/4/3.
 */

public class ClearDialog extends DialogFragment implements View.OnClickListener {

    private ClearClickListener clearClickListener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.no_bg_dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog.setContentView(R.layout.dialog_clear);
        dialog.setCanceledOnTouchOutside(false);

        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = dialog.getWindow();
        //dialog.getWindow().getAttributes().windowAnimations = R.style.dialog_anim;

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER; // 紧贴底部
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT; // 宽度持平
        lp.alpha = 1.0f;
        window.setAttributes(lp);

        dialog.findViewById(R.id.tv_cancel).setOnClickListener(this);
        dialog.findViewById(R.id.tv_ok).setOnClickListener(this);

        return dialog;
    }

    public void setClearClickListener(ClearClickListener clearClickListener) {
        this.clearClickListener = clearClickListener;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_cancel) {
            dismissAllowingStateLoss();
        } else if (id == R.id.tv_ok) {
            dismissAllowingStateLoss();
            if (clearClickListener != null) {
                clearClickListener.onClearClick();
            }
        }
    }

    public interface ClearClickListener {
        void onClearClick();
    }
}
